package com.example.yijian.edit.view.popup;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.yijian.R;
import com.example.yijian.utils.datainfo.ClipInfo;

import static com.example.yijian.utils.Constants.VIDEOVOLUME_MAXSEEKBAR_VALUE;
import static com.example.yijian.utils.Constants.VIDEOVOLUME_MAXVOLUMEVALUE;

public class VolumePopup extends BaseCommonPopupWindow {
    private SeekBar mVolumeSeekBar;
    private TextView mVolumeSeekBarValue;
    private ImageView ivFinish;

    public VolumePopup(Context context) {
        super(context);
        setContentView(R.layout.popup_volume);
        initView();
    }

    private void initView() {
        mVolumeSeekBar = findViewById(R.id.volume_seek);
        mVolumeSeekBarValue = findViewById(R.id.tv_seek_num);
        ivFinish = findViewById(R.id.iv_volume_finish);
        mVolumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    updateVolumeSeekBarValue(progress);
                    if (onFunctionListener != null) {
                        onFunctionListener.onVolumeChanged(VIDEOVOLUME_MAXVOLUMEVALUE * progress / VIDEOVOLUME_MAXSEEKBAR_VALUE);
                    }
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        ivFinish.setOnClickListener(v -> dismiss());
    }

    public void setData(ClipInfo clipInfo) {
        int value = (int) Math.floor(clipInfo.getVolume() * VIDEOVOLUME_MAXSEEKBAR_VALUE / VIDEOVOLUME_MAXVOLUMEVALUE + 0.5D);
        updateVolumeSeekBarValue(value);
    }

    private void updateVolumeSeekBarValue(int value) {
        mVolumeSeekBar.setProgress(value);
        mVolumeSeekBarValue.setText(String.valueOf(value));
    }

    private OnFunctionListener onFunctionListener;

    public void setOnFunctionListener(OnFunctionListener callBack) {
        this.onFunctionListener = callBack;
    }

    public interface OnFunctionListener {
        void onVolumeChanged(float volume);
    }

}
